package com.studentManagement.daoImpl;

import com.studentManagement.dao.DormitoryDao;
import com.studentManagement.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/29 0029.
 */
public class DormitoryDaoImpl implements DormitoryDao {
    private Connection conn = JdbcUtil.getConnection();
    private ResultSet resultSet = null;
    private PreparedStatement preparedStatement = null;

    @Override//通过dormitory_teacher_id查询dormitorys表的dormitory_id
    public List<String> queryDormitory_idByDormitory_teacher_id(String dormitory_id) {
        List<String> list = new ArrayList<>();
        String sql = "SELECT dormitory_id  FROM dormitorys WHERE dormitory_teacher_id=?";
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, dormitory_id);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(resultSet.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeStatementAndResultSet(resultSet, preparedStatement);
            JdbcUtil.closeConnection(conn);
        }
        return list;
    }

    @Override
    public List<String> queryDormitory_idByClass_id(String class_id) {
        String sql = "select dormitory_id from dormitorys where dormitory_class_id=?";
        List<String> list = new ArrayList<>();
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, class_id);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(resultSet.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeStatementAndResultSet(resultSet, preparedStatement);
            JdbcUtil.closeConnection(conn);
        }

        return list;
    }
}
